<?php

/**
 * Description of googleapi
 * 
 * @author anhducbkhn
 * @since Oct 3, 2013 - 4:01:01 PM
 */
require_once (dirname(__FILE__) . '/lib/Google_Client.php');
require_once (dirname(__FILE__) . '/lib/contrib/Google_CalendarService.php');

class Googleapi extends CComponent
{

    public $gClient;
    public $clientID;
    public $clientSecret;
    public $redirectUrl;
    public $applicationName;
    public $services = array('calendar');
    public $calService; //calendar service
    public $cal;

    public function getInstanceClient($redirect_url = NULL)
    {
        $client = $this->gClient;
        $client->setApplicationName($this->applicationName);
        $client->setClientId($this->clientID);
        $client->setClientSecret($this->clientSecret);
        if ($redirect_url)
        {
            $client->setRedirectUri($redirect_url);
        }
        else
        {
            $client->setRedirectUri('http://webmaster.com.sg/calendar/google/');
        }
        $client->setScopes('https://www.googleapis.com/auth/calendar');

        if (isset($_GET['code']))
        {
            $client->authenticate($_GET['code']);
            $_SESSION['token'] = $client->getAccessToken();
            $return_url = $redirect_url ? $redirect_url : Yii::app()->createUrl('calendar/google');
            header('Location:' . $return_url);
        }

        if (isset($_GET['logoutGAccount']))
        {
            unset($_SESSION['token']);
            Google::model()->deleteAll('user_id=:user_id', array(':user_id' => Yii::app()->user->getId()));
            if ($redirect_url)
            {
                Yii::app()->request->redirect(Yii::app()->createUrl($redirect_url));
            }
            else
            {
                Yii::app()->request->redirect(Yii::app()->createUrl('calendar/google'));
            }
        }
        if (isset($_SESSION['token']))
        {
            $client->setAccessToken($_SESSION['token']);

            $client_google = Google::model()->find('user_id=:user_id', array(':user_id' => Yii::app()->user->getId()));
            if ($client_google)
            {
                $client_google->user_id = Yii::app()->user->getId();
                $client_google->token_key = $_SESSION['token'];
                $client_google->save();
            }
            else
            {
                $client_google = new Google();
                $client_google->user_id = Yii::app()->user->getId();
                $client_google->token_key = $_SESSION['token'];
                $client_google->save();
            }
        }
        return $client;
    }

    public function getInstanceCalendarService()
    {
        if ($this->calService == NULL)
        {
            return $this->calService = new Google_CalendarService($this->gClient);
        }
        else
        {
            return $this->calService;
        }
    }

    public function init()
    {
        $this->gClient = new Google_Client();
//        $this->gClient->isAccessTokenExpired()
//        $this->gClient->setState('offline');
//        $this->gClient->setAccessType('offline');

        $client_google = Google::model()->find('user_id=:user_id', array(':user_id' => Yii::app()->user->getId()));

        if ($client_google)
        {
            $this->gClient->setAccessToken($client_google->token_key);
        }
        if ($this->calService != NULL)
        {
            foreach ($this->calService as $s)
            {
                switch ($s)
                {
                    case 'calendar':
                        $this->calService = new Google_CalendarService($this->gClient);

                        break;

                    default:
                        break;
                }
            }
        }
    }

    public function test()
    {
        return $this->clientID;
    }

}

/**
 * End of googleapi
 */